<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity {
	public $user_detail = null;
	
	/**
	 * Authenticates a user.
	 * The example implementation makes sure if the username and password
	 * are both 'demo'.
	 * In practical applications, this should be changed to authenticate
	 * against some persistent user identity storage (e.g. database).
	 *
	 * @return boolean whether authentication succeeds.
	 */
	public function authenticate() {
		/*
		 * $attrs = array ('username' => $this->username, 'password' => $this->password ); $user = TblUser::model ()->findByAttributes ( $attrs, array ('limit' => 1 ) ); $this->user_detail = $user ? $user->attributes : $this->user_detail; return $this->user_detail;
		 */
		$username = strtolower ( $this->username );
		$user = User::model ()->find ( 'LOWER(username)=?', array (
				$username 
		) );
		
		if (empty ( $user ))
			$this->errorCode = self::ERROR_USERNAME_INVALID;
		else if (! $user->validatePassword ( $this->password ))
			$this->errorCode = self::ERROR_PASSWORD_INVALID;
		else {
			$this->errorCode = self::ERROR_NONE;
			$this->user_detail = $user->toArray ();
		}
		return $this->errorCode == self::ERROR_NONE;
	}
	/**
	 * php user identify key.
	 *
	 * @see CUserIdentity::getId()
	 */
	/* public function getId() {
		$str = file_get_contents ( ROOT_DIR . '/protected/data/sec' );
		return sha1 ( $this->username . $str . $this->username );
	} */
}